//
//  SEProfileRecordCell.swift
//  SmartEyeForPatient
//
//  Created by qdsg on 2021/4/13.
//

import UIKit

protocol SEProfileRecordCellDelagete: NSObjectProtocol {
    func profileRecordCell(viewModel: SEPatientDetailViewModel, cellType: SEProfileRecordCvCellType)
}

class SEProfileRecordCell: UITableViewCell {

    static let className = "SEProfileRecordCell"
    static let reuseId = className

    weak var delegate: SEProfileRecordCellDelagete?

    var viewModel = SEProfileViewModel()

    @IBOutlet weak var collectionView: UICollectionView! {
        didSet {
            collectionView.register(UINib(nibName: SEProfileRecordCvCell.className, bundle: nil), forCellWithReuseIdentifier: SEProfileRecordCvCell.reuseId)
        }
    }

    @IBOutlet weak var bgView: UIView! {
        didSet {
            bgView.setLayerCorners(rect: bgView.bounds, radius: 14, corners: [.topLeft, .topRight])
            bgView.setShadow()
        }
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    func updateCell(viewModel: SEProfileViewModel) {
        self.viewModel = viewModel
        collectionView.reloadData()
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

extension SEProfileRecordCell: UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return viewModel.numberOfItemsForPatientCollectionView
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: SEProfileRecordCvCell.reuseId, for: indexPath) as! SEProfileRecordCvCell
        cell.updateCell(viewModel.patientViewModel(indexPath: indexPath), viewModel.colletionViewCellType[indexPath.row])
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: UIScreen.screenWidth - 30, height: 80)
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let cellType = viewModel.colletionViewCellType[indexPath.item]
        let vm = viewModel.patientViewModel(indexPath: indexPath)
        delegate?.profileRecordCell(viewModel: vm, cellType: cellType)
    }
}
